package cn.com.ebidding.controller;

import cn.com.ebidding.business.enums.FileUploadType;
import cn.com.ebidding.core.websocket.server.ZydWebsocketServer;
import cn.com.ebidding.core.websocket.util.WebSocketUtil;
import cn.com.ebidding.plugin.file.GlobalFileUploader;
import cn.com.ebidding.util.ResultUtil;
import cn.com.ebidding.business.annotation.BussinessLog;
import cn.com.ebidding.business.service.BizArticleService;
import cn.com.ebidding.business.service.SysConfigService;
import cn.com.ebidding.file.FileUploader;
import cn.com.ebidding.file.entity.VirtualFile;
import cn.com.ebidding.framework.object.ResponseVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * 其他api性质的接口
 *
 * @author 扫地僧 (saods@ebidding.com.cn)
 * @version 1.0
 * @website https://ksp.ebidding.com.cn
 * @date 2018/4/24 14:37
 * @since 1.0
 */
@RestController
@RequestMapping("/api")
public class RestApiController {

    @Autowired
    private BizArticleService articleService;
    @Autowired
    private SysConfigService configService;
    @Autowired
    private ZydWebsocketServer websocketServer;

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @RequiresPermissions("article:publish")
    @PostMapping("/uploadFile")
    public ResponseVO uploadFile(@RequestParam("file") MultipartFile file,boolean isAttachment) {
        FileUploader uploader = new GlobalFileUploader();
        VirtualFile virtualFile = null;
        if(isAttachment){
            virtualFile = uploader.uploadAttachment(file, FileUploadType.SIMPLE.getPath(), true);
        }else{
            virtualFile = uploader.upload(file, FileUploadType.SIMPLE.getPath(), true);
        }
        return ResultUtil.success("上传成功", virtualFile.getFilePath());
    }
    /**
    * @Description:
    * @Param: [file, isAttachment]  isAttachment: 不传此参数则默认为false。
    * @return: java.lang.Object
    * @Author: wanfy
    * @Date: 2019/3/4 11:39 AM
    */
    @RequiresPermissions("article:publish")
    @PostMapping("/uploadAttachment")
    public Object uploadAttachment(@RequestParam("file") MultipartFile[] files,boolean isAttachment) {
        FileUploader uploader = new GlobalFileUploader();
        VirtualFile virtualFile = null;
        int fileCount = files.length;
        Long[] filePathArr = new Long[fileCount];
        for(int i= 0;i<fileCount;i++ ){
            MultipartFile file = files[i];
            if(isAttachment){
                virtualFile = uploader.uploadAttachment(file, FileUploadType.SIMPLE.getPath(), true);
            }else{
                virtualFile = uploader.upload(file, FileUploadType.SIMPLE.getPath(), true);
            }
            filePathArr[i] =  virtualFile.getId();
        }


        Map<String, Object> resultMap = new HashMap<>(3);
        resultMap.put("success", 1);
        resultMap.put("message", "上传成功");
        resultMap.put("fileIdArray", filePathArr);
        return resultMap;
    }

    /**
     * @Description:
     * @Param: [file, isAttachment]  isAttachment: 不传此参数则默认为false。
     * @return: java.lang.Object
     * @Author: wanfy
     * @Date: 2019/3/4 11:39 AM
     */
    @RequiresPermissions("article:publish")
    @PostMapping("/uploadFileForMd")
    public Object uploadFileForMd(@RequestParam("file") MultipartFile[] files,boolean isAttachment) {
        FileUploader uploader = new GlobalFileUploader();
        VirtualFile virtualFile = null;
        int fileCount = files.length;
        for(int i= 0;i<fileCount;i++ ){
            MultipartFile file = files[i];
            if(isAttachment){
                virtualFile = uploader.uploadAttachment(file, FileUploadType.SIMPLE.getPath(), true);
            }else{
                virtualFile = uploader.upload(file, FileUploadType.SIMPLE.getPath(), true);
            }
        }


        Map<String, Object> resultMap = new HashMap<>(3);
        resultMap.put("success", 1);
        resultMap.put("message", "上传成功");
        resultMap.put("filename", "/file/get/"+virtualFile.getFilePath());
        return resultMap;
    }

    /**
     * 发送消息通知
     *
     * @return
     */
    @RequiresPermissions("notice")
    @PostMapping("/notice")
    @BussinessLog("通过websocket向前台用户发送通知")
    public ResponseVO notice(String msg) throws UnsupportedEncodingException {
        WebSocketUtil.sendNotificationMsg(msg, websocketServer.getOnlineUsers());
        return ResultUtil.success("消息发送成功");
    }

}
